// pages/addCar/addFrom/index.js
import { drivingLicense,insertOrUpdateCar,carDetail } from "../../../api/addCar/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [{
        text: '京',
        value: 0
      },
      {
        text: '沪',
        value: 1
      },
      {
        text: '浙',
        value: 2
      },
      {
        text: '苏',
        value: 3
      },
      {
        text: '粤',
        value: 4
      },
      {
        text: '冀',
        value: 5
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },
      {
        text: '冀',
        value: 2
      },

    ],
    value1: 0,
    show: false,
    date: '',
    date1:'',
    checked: false,
    carNo:'',
    issuesY:'',
    issuesM:'',
    issuesD:'',
    registrationY:'',
    registrationM:'',
    registrationD:'',
    carBrand:'',
    vinNo:'',
    engineNo:'',
    carMark:'',
    id:null
  },
  onClose() {
    this.setData({
      show: false,
      id:null
    });
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    const arr = e.detail.value.split('-')
    this.setData({
      date: e.detail.value,
      issuesY:arr[0],
      issuesM:arr[1],
      issuesD:arr[2],
    })
  },
  bindDateChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    const arr = e.detail.value.split('-')
    this.setData({
      date1: e.detail.value,
      registrationY:arr[0],
      registrationM:arr[1],
      registrationD:arr[2],
    })
  },
  onChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      checked: detail
    });
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  onDateFocus() {
    this.setData({
      show: true
    });
  },
  chooseCarClass() {
    wx.navigateTo({
      url: '/pages/addCar/carClass/index',
    })
  },
  handleChooseImage() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success(res) {
        wx.uploadFile({
          url: 'https://chezhu.prd4.weiniaokeji.cn/oss/file', //仅为示例，非真实的接口地址
          filePath:res.tempFiles[0].tempFilePath,
          name: 'file',
          success(res) {
            console.log(res)
            const data = JSON.parse(res.data) 
            console.log(data)
            drivingLicense({url:data.data}).then(res=>{
              console.log(res)
              const issues = res.data.data.issueDate.split('-')
              const registrationDates = res.data.data.registrationDate.split('-')
              that.setData({
                carNo:res.data.data.licensePlateNumber,
                vinNo:res.data.data.vinCode,
                engineNo:res.data.data.engineNumber,
                issuesY:issues[0],
                issuesM:issues[1],
                issuesD:issues[2],
                registrationY:registrationDates[0],
                registrationM:registrationDates[1],
                registrationD:registrationDates[2]
              })
                app.globalData.carInfo = {
                  carNo:res.data.data.licensePlateNumber,
                vinNo:res.data.data.vinCode,
                engineNo:res.data.data.engineNumber,
                issuesY:issues[0],
                issuesM:issues[1],
                issuesD:issues[2],
                registrationY:registrationDates[0],
                registrationM:registrationDates[1],
                registrationD:registrationDates[2]
                }
            })
            //do something
          }
        })
      }
    })
  },
  handleSubmit(){
    insertOrUpdateCar({
      carNo:this.data.carNo,
      carBrand:this.data.carBrand,
      carMark:this.data.carMark,
      vinNo:this.data.vinNo,
      engineNo:this.data.engineNo,
      registerTime:this.data.registrationY + '-' + this.data.registrationM + "-" + this.data.registrationD,
      checkValidity:this.data.issuesY + '-' + this.data.issuesM + "-" + this.data.issuesD,
      isDefault:this.data.checked,
      id:this.data.id
    }).then(res=>{
      console.log(res)
      app.globalData.carInfo = null

      wx.navigateTo({
        url: '/pages/my/car/index',
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.ids)

    if(options.ids){
      carDetail({
        id:options.ids
      }).then(res=>{
        console.log(res)
        this.setData({
          carNo:res.data.data.carNo,
          carBrand:res.data.data.carBrand,
          vinNo:res.data.data.vinNo,
          engineNo:res.data.data.engineNo,
          issuesY:res.data.data.registerTime.split("-")[0],
          issuesM:res.data.data.registerTime.split("-")[1],
          issuesD:res.data.data.registerTime.split("-")[2],
          registrationY:res.data.data.checkValidity.split("-")[0],
          registrationM:res.data.data.checkValidity.split("-")[1],
          registrationD:res.data.data.checkValidity.split("-")[2],
          date1:res.data.data.checkValidity,
          date:res.data.data.registerTime,
          carMark:res.data.data.carMark,
          id:res.data.data.id,
          checked:res.data.data.isDefault
        })
      })
    }


    if(options.logo){
      this.setData({
        carMark:options.logo
      })
    }
    if(options.name){
      this.setData({
        carBrand:options.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(app)
    if(app.globalData.carInfo){
      this.setData({
        carNo:app.globalData.carInfo.carNo,
        vinNo:app.globalData.carInfo.vinNo,
        engineNo:app.globalData.carInfo.engineNo,
        issuesY:app.globalData.carInfo.issuesY,
        issuesM:app.globalData.carInfo.issuesM,
        issuesD:app.globalData.carInfo.issuesD,
        registrationY:app.globalData.carInfo.registrationY,
        registrationM:app.globalData.carInfo.registrationM,
        registrationD:app.globalData.carInfo.registrationD
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})